import {onMounted, ref} from "vue";
import {addUserByAdmin, deleteUserById, getUserList} from "@/module-base/api/sysManage.js";
import {Message,Modal} from "view-ui-plus";
import {useUserStore} from '@module-base/stores/userStore.js'

export function useUserHandle(){
    const {openModal}=useUserStore()
    const userData = ref([])
    const userInfoForm=ref({})

    onMounted(() => {
        handleGetList()
    })

    async function editUser(row) {
        console.log('editUser',row)
        const id=row.id
        console.log(row)
        userInfoForm.value=row
        openModal()
        // const editUser=await getUserById(id)
    }
    async function deleteUser(id) {
        // const id=row.id
        console.log(id)
        if(!id){
            Message.error('请选择要删除的数据')
            return
        }
        Modal.confirm({
            title: '删除用户',
            content: '<p>请确认是否删除选中的用户</p>',
            loading: true,
            onOk: async () => {
                const rs=await deleteUserById(id)
                if(rs.success){
                    handleGetList()
                    Message.success('删除成功')
                    Modal.remove()
                }
            }
        });

    }
    async function handleGetList(pageIndex, pageSize) {
        const rs = await getUserList({
            pageIndex: pageIndex || 1,
            pageSize: pageSize || 20
        })
        if (rs.success) {
            userData.value = rs.data
        }
    }
    return {
        userData,
        userInfoForm,
        editUser,
        deleteUser,
        handleGetList
    }
}